package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmNews;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-21
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {

//    敏感词汇的分液查询
    @Override
    public ResponseResult find(SensitiveDto sensitiveDto) {
        IPage<WmSensitive> page=new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());

        LambdaQueryWrapper<WmSensitive> query=new LambdaQueryWrapper<>();
            if(sensitiveDto.getSensitives()!=null){
                query.like(WmSensitive::getSensitives,sensitiveDto.getSensitives());
            }
        IPage<WmSensitive> iPage = this.page(page, query);
        List<WmSensitive> records = iPage.getRecords();
        long total = iPage.getTotal();
        PageResponseResult<List<WmSensitive>> responseResult = new PageResponseResult<>(sensitiveDto.getPage(), sensitiveDto.getSize(), total,records);
        return responseResult;



    }
//    敏感词汇的增加

    @Override
    public ResponseResult add(WmSensitive wmSensitive) {
//        非空校验
        if(StringUtils.isEmpty(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);

        }
        wmSensitive.setCreatedTime(new Date());
        this.save(wmSensitive);
        return ResponseResult.okResult();
    }

}
